<%@ page trimDirectiveWhitespaces="true" %>
<%@page import="com.jason.imagesnode.MyStringUtil"%>
<%@page import="org.json.JSONObject"%>
<%@page import="java.util.UUID"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page contentType="text/html; charset=UTF-8"%>

<%@ page import="sun.misc.BASE64Decoder"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.io.File"%>

<%
	String sId = UUID.randomUUID().toString();
	String fileName = "";
	response.setHeader("Access-Control-Allow-Origin","*");

	JSONObject obj = new JSONObject();
	obj.put("result", "Y");
	boolean flag = true;
	try {
		request.setCharacterEncoding("UTF-8");
		
		String postfixName = (String)request.getParameter("postfixName");// 服务器前缀
		if(MyStringUtil.isBlank(postfixName)){
			postfixName = "";
		}
		
		String content = (String)request.getParameter("file");
		content = content.substring(23);
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();    
		byte[] buffer = decoder.decodeBuffer(content);
		
		String basePath = request.getServletContext().getRealPath("/");
		String filename = UUID.randomUUID().toString().replace("-", "");
		filename = MyStringUtil.getFileName(filename,postfixName);
		String filepath = MyStringUtil.getFilePath(filename);
		File f = new File(basePath + filepath);
		if (!f.exists()) {
			f.mkdirs();
		}
		
		String filenames = filename + ".png";// 文件名
		
		FileOutputStream outfile = new FileOutputStream(basePath + filepath+"/" + filename + ".png");
		outfile.write(buffer);
		outfile.close();
		
		System.out.println(basePath + filepath+"/" + filename + ".png");
		
		obj.put("filename",filenames);
	} catch (Exception e) {
		obj.put("errMsg", e);
		obj.put("result", "N");
		e.printStackTrace();
	} finally {
		try {
			
		} catch (Exception e) {
		}
		//out.println("Access-Control-Allow-Origin:*\r\n");
		out.println(obj.toString());
	}
%>
